# 常量配置中心

import configparser
from pathlib import Path


class ConfigHandler:
    USER_AGENTS = [
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
        'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
        'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'
    ]

    REQUEST_TIMEOUT = 30
    COOKIE_UPDATE_INTERVAL = (120, 180)
    BASE_URL = 'https://www.amazon.com'
    DEFAULT_ZIP_CODE = '92337'
    LOAD_PAGE_TIMEOUT = 30

    def __init__(self):
        self.config = configparser.ConfigParser()
        # 假设配置文件在项目根目录，根据实际情况修改
        config_path = Path(__file__).parent / 'config.ini'
        self.config.read(str(config_path))

    def get_database_config(self):
        return {
            'database': self.config['database']['name'],
            'user': self.config['database']['user'],
            'password': self.config['database']['password'],
            'host': self.config['database']['host'],
            'port': int(self.config['database']['port'])
        }

    def get_browser_config(self):
        return {
            'driver_count': int(self.config['browser']['driver_count']),
            'headless': self.config.getboolean('browser', 'headless', fallback=False),
        }

    def get_logging_config(self):
        return {
            'log_dir': self.config.get('logging', 'log_dir', fallback='logs'),
            'console_logging': self.config.getboolean('logging', 'console_logging', fallback=True),
            'max_bytes': self.config.getint('logging', 'max_bytes', fallback=10485760),
            'backup_count': self.config.getint('logging', 'backup_count', fallback=5)
        }

    def get_crawl_config(self):
        return {
            'max_exec_times': self.config.getint('crawl', 'max_exec_times', fallback=10),
            'sched_interval': self.config.getint('crawl','sched_interval', fallback=60),
            'max_workers': self.config.getint('crawl', 'max_workers', fallback=3),
        }

config = ConfigHandler()


if __name__ == '__main__':
    print('数据库配置:', config.get_database_config())
    print('浏览器配置:', config.get_browser_config())